package com.application.lucky.widget.dialog;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.application.lucky.R;
import com.application.lucky.bean.eventbean.FriendEvent;
import com.application.lucky.manager.UserManagers;
import com.application.lucky.request.FriendRequest;
import com.application.lucky.request.LoginRequest;
import com.application.mylibrary.utils.EventBusUtils;
import com.baselib.libnetwork.net.libnetworkcomponent.BaseResponse;

public class VerificationFriendDialog extends Dialog implements View.OnClickListener {

    private final FriendRequest friendRequest;
    private TextView tvVerFriendDialogTitle;
    private TextView tvEtTitle;
    private EditText etInputView;
    private TextView tvDialogLeftBt;
    private TextView tvDialogRightBt;
    private boolean isAdd;

    public VerificationFriendDialog(@NonNull Context context, boolean isAdd) {
        super(context, R.style.dialog);
        this.isAdd = isAdd;
        friendRequest = new FriendRequest();

        View view = LayoutInflater.from(context).inflate(R.layout.dialog_ver_friend_view, null);
        tvVerFriendDialogTitle = view.findViewById(R.id.tv_ver_friend_dialog_title);
        tvEtTitle = view.findViewById(R.id.tv_et_title);
        etInputView = view.findViewById(R.id.et_input_view);
        tvDialogLeftBt = view.findViewById(R.id.tv_dialog_left_bt);
        tvDialogRightBt = view.findViewById(R.id.tv_dialog_right_bt);
        initView();
        setContentView(view);

    }

    private String userId;

    public void setUserId(String userId) {
        this.userId = userId;
    }

    private void initView() {
        if (isAdd) {
            tvEtTitle.setVisibility(View.GONE);
            tvVerFriendDialogTitle.setText("验证消息");
        } else {
            tvEtTitle.setVisibility(View.VISIBLE);
            tvVerFriendDialogTitle.setText("确认通过验证？");
        }
        tvDialogLeftBt.setOnClickListener(this);
        tvDialogRightBt.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_dialog_left_bt:
                dismiss();
                break;
            case R.id.tv_dialog_right_bt:
                if (isAdd) {
                    add(userId);
                } else {
                    comint(userId);
                }
                break;

        }
    }

    private void comint(final String userId) {
        friendRequest.friend_agree(userId, "1111", new LoginRequest.LoginBack<BaseResponse<String>>() {
            @Override
            public void success(BaseResponse<String> stringBaseResponse) {
                if (listener != null) {
                    listener.success(userId);
                    String s = etInputView.getText().toString();
                    if (!TextUtils.isEmpty(s)) {
                        friendRequest.friend_remarks(userId, s, null);
                    }
                    EventBusUtils.post(new FriendEvent());
                }
            }

            @Override
            public void fail(int code) {
                if (listener != null) {
                    listener.fail(userId, code);
                }
            }
        });
    }

    private void add(final String userId) {
        String s = etInputView.getText().toString();
        if (TextUtils.isEmpty(s)) {
            s = UserManagers.getInstance().getLoginName();
        }
        friendRequest.friend_apply(userId, "1", s, "2131", new LoginRequest.LoginBack<BaseResponse<String>>() {
            @Override
            public void success(BaseResponse<String> stringBaseResponse) {
                if (listener != null) {
                    listener.success(userId);
                }
            }

            @Override
            public void fail(int code) {
                if (listener != null) {
                    listener.fail(userId, code);
                }
            }
        });
    }

    public void showDialog() {
        if (etInputView != null) {
            etInputView.setText("");
        }
        show();
    }

    private AddListener listener;

    public void setListener(AddListener listener) {
        this.listener = listener;
    }

    public interface AddListener {
        void success(String id);

        void fail(String uid, int code);
    }
}
